package org.whuims.web.news;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;
import org.whuims.entity.News;
import org.whuims.entity.User;
import org.whuims.service.account.ShiroDbRealm;
import org.whuims.service.news.NewsService;

import javax.servlet.ServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Qikai on 2016/11/7.
 */
@Controller
@RequestMapping(value = "/news")
public class NewsController {

    private static final String PAGE_SIZE = "5";
    @Autowired
    private NewsService newsService;

    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String createForm(Model model) {
        News news = new News();
        news.setPublishDate(new Date());
        model.addAttribute("news", news);
        model.addAttribute("action", "create");
        return "news/newsedit";
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
                       @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize, Model model,
                       ServletRequest request) {
        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
        Page<News> newsList = newsService.getNewsList(searchParams, pageNumber, pageSize, null);
        model.addAttribute("newsPage", newsList);
        model.addAttribute("sortType", "auto");
        // 将搜索条件编码成字符串，用于排序，分页的URL
        model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
        return "news/newslist";
    }

    @RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
    public String detail(@PathVariable("id") Long id, Model model) {
        News news = newsService.getNews(id);
        List<News> newNews = newsService.findTopNews(6);
        model.addAttribute("news", news);
        model.addAttribute("newNews", newNews);
        return "news/newsdetail";
    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public String create(News news, RedirectAttributes redirectAttributes) {
        User user = new User(getCurrentUserId());
        news.setAuthor(user);
        newsService.saveNews(news);
        redirectAttributes.addFlashAttribute("message", "创建任务成功");
        return "redirect:/news/detail/" + news.getId();
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(@Valid @ModelAttribute("news") News news, RedirectAttributes redirectAttributes) {
        User user = new User(getCurrentUserId());
        news.setAuthor(user);
        newsService.saveNews(news);
        redirectAttributes.addFlashAttribute("message", "更新成功");
        return "redirect:/news/detail/" + news.getId();
    }

    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updateForm(@PathVariable("id") Long id, Model model) {
        News news = newsService.getNews(id);
        model.addAttribute("action", "update");
        model.addAttribute(news);
        return "news/newsedit";
    }

    /**
     * 取出Shiro中的当前用户Id.
     */
    private Long getCurrentUserId() {
        ShiroDbRealm.ShiroUser user = (ShiroDbRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        return user.id;
    }
}
